package com.fastdevelop.framework.base;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.fastdevelop.framework.utils.Constant;

import java.util.Arrays;

public class BaseActivity extends AppCompatActivity {

    protected Activity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = this;
    }

    /**
     * 申请危险权限
     */
    public final void requestDangerPermissions(String... permissions) {
        if (permissions.length == 0)
            return;

        if (permissions.length == 1) {
            //如果没有授权
            if (ContextCompat.checkSelfPermission(mActivity, permissions[0]) != PackageManager.PERMISSION_GRANTED) {
                //如果第一次申请的权限已经拒绝了，并且第二次权限申请的时候显示自己的Dialog提示用户为什么需要这个权限
                if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permissions[0])) {
                    requestPermissionsCallback(Constant.RequestPermissionResult.SHOULD_SHOW_RATIONALE, Constant.RequestPermissionType.SINGLE, permissions);
                } else {
                    ActivityCompat.requestPermissions(mActivity, permissions, /*REQUEST_PERMISSION_SINGLE*/0);
                }
            } else {
                //已授权
                requestPermissionsCallback(Constant.RequestPermissionResult.SUCCESS, Constant.RequestPermissionType.SINGLE, permissions);
            }
        } else {
            if (isAllGranted(permissions)) {
                //所有权限都已授权
                requestPermissionsCallback(Constant.RequestPermissionResult.SUCCESS, Constant.RequestPermissionType.MANY, permissions);
            } else {
                ActivityCompat.requestPermissions(mActivity, permissions, /*REQUEST_PERMISSION_MANY*/0);
            }
        }
    }

    /**
     * 重新申请权限
     */
    public final void againRequestPermission(String... permissions) {
        if (permissions.length == 0)
            return;
        ActivityCompat.requestPermissions(mActivity, permissions, /*permissions.length == 1 ? REQUEST_PERMISSION_SINGLE : REQUEST_PERMISSION_MANY*/0);
    }

    /**
     * 判断是否相同的权限
     */
    public final boolean isPermissionEquals(String[] permissions,String requestPermissionName){
        if(permissions.length == 1)
            return permissions[0].equals(requestPermissionName);
        else
            return Arrays.asList(permissions).contains(requestPermissionName);
    }

    /**
     * 多个申请的权限是否全部授权
     */
    private boolean isAllGranted(String[] permissions) {
        int grantedCounts = 0;
        for (String permissionName : permissions) {
            if (ContextCompat.checkSelfPermission(mActivity, permissionName) == PackageManager.PERMISSION_GRANTED)
                grantedCounts += 1;
        }
        return grantedCounts == permissions.length;
    }

    @Override
    public final void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (permissions.length == 1) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //用户允许授权
                requestPermissionsCallback(Constant.RequestPermissionResult.SUCCESS, Constant.RequestPermissionType.SINGLE, permissions);
            } else {
                //用户拒绝授权
                requestPermissionsCallback(Constant.RequestPermissionResult.REJECT, Constant.RequestPermissionType.SINGLE, permissions);
            }
            return;
        } else {
            if (isAllGranted(permissions)) {
                requestPermissionsCallback(Constant.RequestPermissionResult.SUCCESS, Constant.RequestPermissionType.MANY, permissions);
            } else {
                requestPermissionsCallback(Constant.RequestPermissionResult.REJECT, Constant.RequestPermissionType.MANY, permissions);
            }
            return;
        }

        /*super.onRequestPermissionsResult(requestCode, permissions, grantResults);*/
    }

    /**
     * 权限申请回调
     */
    public void requestPermissionsCallback(Constant.RequestPermissionResult requestPermissionResult, Constant.RequestPermissionType requestPermissionType, String[] permissions) {
        //Empty implement,You can override this method to implement your logic.
    }
}
